/*****************************************************************
*  Copyright (C) 2012  Marco Bavagnoli - lil.deimos@gmail.com    *
******************************************************************/

#include "camfeatures.h"

#include <QGraphicsVideoItem>
#include <QCameraImageCapture>

cameraFeatures::cameraFeatures(QDeclarativeItem *parent) :
    QDeclarativeItem(parent),
    m_camera(0),
    m_featuredAcquired(false)
{
    // Get all available camera devices
    foreach (const QByteArray array, QCamera::availableDevices()) {
        availableDevices << QString(array);
    }
}

void cameraFeatures::startGetFeatures(QString device)
{
    m_featuredAcquired=false;
    m_camera = new QCamera(device.toLatin1(), this);
    connect(m_camera, SIGNAL(stateChanged(QCamera::State)), this, SLOT(getFeatures(QCamera::State)));
    m_camera->load();
}

//////////////////////////////////////////////////////
// Retrieve available camera features functions
void cameraFeatures::getFeatures(QCamera::State state)
{
    if (state == QCamera::UnloadedState) {
        if ( m_featuredAcquired ) {
            delete m_camera;
            m_camera=0;
            emit featuresRetrieved();
            return;
        }
    }

    QCameraImageCapture *cameraImageCapture = new QCameraImageCapture(m_camera);
    availableResolutionsSpeedModes = cameraImageCapture->supportedResolutions();
    delete cameraImageCapture;

    // Get all available exposure modes
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureManual)) availableExposures << ExposureManual;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureAuto)) availableExposures << ExposureAuto;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureNight)) availableExposures << ExposureNight;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureBacklight)) availableExposures << ExposureBacklight;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureSpotlight)) availableExposures << ExposureSpotlight;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureSports)) availableExposures << ExposureSports;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureSnow)) availableExposures << ExposureSnow;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureBeach)) availableExposures << ExposureBeach;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureLargeAperture)) availableExposures << ExposureLargeAperture;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureSmallAperture)) availableExposures << ExposureSmallAperture;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposurePortrait)) availableExposures << ExposurePortrait;
    if (m_camera->exposure()->isExposureModeSupported(QCameraExposure::ExposureModeVendor)) availableExposures << ExposureModeVendor;

    // Get all available flash modes
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashOff)) availableFlashModes << FlashOff;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashOn)) availableFlashModes << FlashOn;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashAuto)) availableFlashModes << FlashAuto;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashRedEyeReduction)) availableFlashModes << FlashRedEyeReduction;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashFill)) availableFlashModes << FlashFill;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashTorch)) availableFlashModes << FlashTorch;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashSlowSyncFrontCurtain)) availableFlashModes << FlashSlowSyncFrontCurtain;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashSlowSyncRearCurtain)) availableFlashModes << FlashSlowSyncRearCurtain;
    if (m_camera->exposure()->isFlashModeSupported(QCameraExposure::FlashManual)) availableFlashModes << FlashManual;

    // Get all available w/b modes
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceAuto)) availableWhiteBalanceModes << WhiteBalanceAuto;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceManual)) availableWhiteBalanceModes << WhiteBalanceManual;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceSunlight)) availableWhiteBalanceModes << WhiteBalanceSunlight;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceCloudy)) availableWhiteBalanceModes << WhiteBalanceCloudy;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceShade)) availableWhiteBalanceModes << WhiteBalanceShade;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceTungsten)) availableWhiteBalanceModes << WhiteBalanceTungsten;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceFluorescent)) availableWhiteBalanceModes << WhiteBalanceFluorescent;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceIncandescent)) availableWhiteBalanceModes << WhiteBalanceIncandescent;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceFlash)) availableWhiteBalanceModes << WhiteBalanceFlash;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceSunset)) availableWhiteBalanceModes << WhiteBalanceSunset;
    if (m_camera->imageProcessing()->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceVendor)) availableWhiteBalanceModes << WhiteBalanceVendor;

    // Get all available isos
    availableIsoModes = m_camera->exposure()->supportedIsoSensitivities();

    // Get all available apertures
    availableApertureModes = m_camera->exposure()->supportedApertures();

    // Get all available isos
    availableShutterSpeedModes = m_camera->exposure()->supportedShutterSpeeds();

    // Fill EV compensations list
    availableCompensationModes << "-2" << "-1.5" << "-1" << "-0.5"
                     << "0" << "+0.5" << "+1" << "+1.5" << "+2";

    m_featuredAcquired=true;
    m_camera->unload();
}

QStringList cameraFeatures::getAvailableDevices()
{
    return availableDevices;
}

bool cameraFeatures::isExposureSupported(ExposureMode state)
{
    return availableExposures.indexOf(state)>=0;
}

bool cameraFeatures::isFlashModeSupported(FlashMode state)
{
    return availableFlashModes.indexOf(state)>=0;
}

bool cameraFeatures::isWhiteBalanceSupported(WhiteBalanceMode state)
{
    return availableWhiteBalanceModes.indexOf(state)>=0;
}

QList<int> cameraFeatures::getIsoModesSupportedList()
{
    return availableIsoModes;
}

QList<qreal> cameraFeatures::getSupportedAperturesList()
{
    return availableApertureModes;
}

QList<qreal> cameraFeatures::getSupportedShutterSpeedsList()
{
    return availableShutterSpeedModes;
}

QStringList cameraFeatures::getCompensationsModes()
{
    return availableCompensationModes;
}

QList<QSize> cameraFeatures::getSupportedResolutionsList()
{
    return availableResolutionsSpeedModes;
}

